package com.blood.modules.system.service.impl;

import cn.hutool.core.lang.Snowflake;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.blood.core.config.mybatisplus.ICommonServiceImpl;
import com.blood.core.resp.GlobalResultStatus;
import com.blood.core.resp.PageResult;
import com.blood.core.resp.ResponseResult;
import com.blood.core.util.ObjectAssertUtil;
import com.blood.core.util.bean.BeanUtil;
import com.blood.core.util.bean.ListUtils;
import com.blood.core.util.http.RequestTheadUtils;
import com.blood.modules.system.dto.req.SysLogPageReq;
import com.blood.modules.system.dto.req.SysLogReq;
import com.blood.modules.system.dto.req.SysLogSaveReq;
import com.blood.modules.system.dto.resp.SysLogInfoResp;
import com.blood.modules.system.dto.resp.SysLogPageResp;
import com.blood.modules.system.mapper.SysLogMapper;
import com.blood.modules.system.model.SysLog;
import com.blood.modules.system.service.ISysLogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collections;
import java.util.List;


/**
 * 日志Service业务层处理
 *
 * @author daoren
 * @date 2022-10-17 10:53:03
 */
@Service
public class SysLogServiceImpl extends ICommonServiceImpl<SysLogMapper, SysLog> implements ISysLogService {
    @Autowired
    private Snowflake snowFlake;

    @Override
    public ResponseResult<PageResult<SysLogPageResp>> pageList(SysLogPageReq req) {
        PageResult<SysLogPageResp> pageResult = PageResult.create(req.getPageNo(), req.getPageSize());
        LambdaQueryWrapper<SysLog> qw = new LambdaQueryWrapper<>();

        qw.orderByDesc(SysLog::getCreateTime);

        IPage<SysLog> iPage = baseMapper.selectPage(new Page<>(req.getPageNo(), req.getPageSize()), qw);
        if (iPage.getRecords().size() > 0) {
            List<SysLogPageResp> queryRespList = ListUtils.copyProperties(iPage.getRecords(), SysLogPageResp.class);
            return GlobalResultStatus.SUCCESS.toResult().response(pageResult.data(iPage.getTotal(), queryRespList));
        }
        return GlobalResultStatus.SUCCESS.toResult().response(pageResult.data(0, Collections.emptyList()));
    }

    @Override
    public List<SysLog> queryList(SysLogReq req) {
        return baseMapper.selectList(new QueryWrapper<SysLog>().lambda().eq(SysLog::isDeleteFlag, false));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResponseResult<Void> saveInfo(SysLogSaveReq req) {
        String id = snowFlake.nextIdStr();
        SysLog saveInfo = new SysLog();
        BeanUtil.copyProperties(req, saveInfo);
        saveInfo.setId(id);
        saveInfo.setCreateBy(RequestTheadUtils.getUserId());
        saveInfo.setUrl(RequestTheadUtils.getUrl());
        saveInfo.setOrgId(RequestTheadUtils.getOrgId());
        baseMapper.insert(saveInfo);
        return GlobalResultStatus.SUCCESS.toResult().responseEmptyBody();
    }


    @Override
    public ResponseResult<SysLogInfoResp> findDetailById(String id) {
        SysLog queryInfo = super.findById(id);
        ObjectAssertUtil.objectIsNull(queryInfo, "详细信息不存在!");
        return GlobalResultStatus.SUCCESS.toResult().response(BeanUtil.copyProperties(queryInfo, SysLogInfoResp.class));
    }
}
